package cn.com.egova.materialdesignexample.activity;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.egova.materialdesignexample.R;

public class CoordinatorLayoutActivity extends AppCompatActivity {
    // 控制ToolBar的变量
    private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR = 0.9f;
    private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS = 0.3f;
    private static final int ALPHA_ANIMATIONS_DURATION = 200;
    private boolean mIsTheTitleVisible = false;
    private boolean mIsTheTitleContainerVisible = true;

    @InjectView(R.id.iv)
    ImageView iv;                           // 大图片
    @InjectView(R.id.ll_title_container)
    LinearLayout llTitleContainer;          // Title的LinearLayout
    @InjectView(R.id.fl_title)
    FrameLayout flTitle;                    // Title的FrameLayout
    @InjectView(R.id.appBarLayout)
    AppBarLayout appBarLayout;              // 整个可以滑动的AppBar
    @InjectView(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;                // 标题栏Title
    @InjectView(R.id.toolbar)
    Toolbar toolbar;                        // 工具栏

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator_layout);
        ButterKnife.inject(this);

        toolbar.setTitle("");

        // AppBar的监听
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                Log.i("verticalOffset:", verticalOffset + "");
                //发现打印的值:往上滑动(0~-900),往下滑动(-900~0),我的模拟器density=2.0,而CollapsingToolbarLayout
                //在xml布局里的高度正好是450dp,AppBarLayout是wrap_content的,因此它的高度也是900px.
                int maxScroll = appBarLayout.getTotalScrollRange();
                Log.i("maxScroll:", maxScroll + "");

                float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
                handleAlphaOnTitle(percentage);
                handleToolbarTitleVisibility(percentage);
            }
        });

//        initParallaxValues(); // 自动滑动效果
    }

    // 设置自动滑动的动画效果
    private void initParallaxValues() {
        CollapsingToolbarLayout.LayoutParams ivLayoutParams =
                (CollapsingToolbarLayout.LayoutParams) iv.getLayoutParams();

        CollapsingToolbarLayout.LayoutParams flTitleLayoutParams =
                (CollapsingToolbarLayout.LayoutParams) flTitle.getLayoutParams();

        //iv,flTitle都是嵌套在CollapsingToolbarLayout里边,但是他们的视差因子不一样,值越大阻尼系数越大,滑动的距离越短
        //正是因为这两个控件的视察因子相差较大,所以就会出现下面的flTitle覆盖住上面iv的效果!
        //注意这个效果在xml布局中设置更简单,已经测试过了!  app:layout_collapseParallaxMultiplier=""
        ivLayoutParams.setParallaxMultiplier(0.9f);
        flTitleLayoutParams.setParallaxMultiplier(0.3f);

        iv.setLayoutParams(ivLayoutParams);
        flTitle.setLayoutParams(flTitleLayoutParams);
    }

    // 处理ToolBar的显示
    private void handleToolbarTitleVisibility(float percentage) {
        if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {
            if (!mIsTheTitleVisible) {
                //对Toolbar上的title设置透明度显示动画.
                startAlphaAnimation(tvToolbarTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                mIsTheTitleVisible = true;
            }
        } else {
            if (mIsTheTitleVisible) {
                startAlphaAnimation(tvToolbarTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                mIsTheTitleVisible = false;
            }
        }
    }

    // 控制Title的显示
    private void handleAlphaOnTitle(float percentage) {
        if (percentage >= PERCENTAGE_TO_HIDE_TITLE_DETAILS) {
            if (mIsTheTitleContainerVisible) {
                //对llTitleContainer设置透明度显示动画.
                startAlphaAnimation(llTitleContainer, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
                mIsTheTitleContainerVisible = false;
            }
        } else {
            if (!mIsTheTitleContainerVisible) {
                startAlphaAnimation(llTitleContainer, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
                mIsTheTitleContainerVisible = true;
            }
        }
    }

    // 设置渐变的动画
    public static void startAlphaAnimation(View v, long duration, int visibility) {
        AlphaAnimation alphaAnimation = (visibility == View.VISIBLE)
                ? new AlphaAnimation(0f, 1f)
                : new AlphaAnimation(1f, 0f);

        alphaAnimation.setDuration(duration);
        alphaAnimation.setFillAfter(true);
        v.startAnimation(alphaAnimation);
    }
}
